package cn.MaySoon.shenfu;

import cn.MaySoon.PlayerLevel.PlayerLevelUpEvent;
import com.germ.germplugin.api.dynamic.gui.GermGuiScreen;
import com.germ.germplugin.api.event.gui.GermGuiClickEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.IOException;

public class listen implements Listener {
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e){
        Player p = e.getPlayer();
        new BukkitRunnable() {

            @Override
            public void run() {
                // TODO 自动生成的方法存根
                main.PlayerShenFuData.put(p, handle.getPlayerData(p));
                if (cn.MaySoon.PlayerLevel.main.PlayerLevel.get(p).getLevel()>=150){
                    if (main.PlayerShenFuData.get(p).level==0){
                       main.PlayerShenFuData.get(p).setPlayerlevel(1);

                        try {
                            handle.setPlayerData(p);
                            p.sendMessage("§a神符功能现已开放~");
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                }
                    jinengap.updateAttribute(p);
                    AP.updatePlayerAttribute(p);
            }
        }.runTaskLaterAsynchronously(main.plugin,80L);

    }

    @EventHandler
    public void onPlayerLevelUp(PlayerLevelUpEvent e){
            Player p = e.getPlayer();
            if (e.getAfterLevel()>=150){
                if (main.PlayerShenFuData.get(p).getPlayerlevel()==0){
                    main.PlayerShenFuData.get(p).setPlayerlevel(1);
                    jinengap.updateAttribute(p);
                    AP.updatePlayerAttribute(p);
                    p.sendMessage("§a恭喜你升级,神符功能已开放!");
                    return;
                }
            }
    }



    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e) {
        Player p = e.getPlayer();
        new BukkitRunnable() {

            @Override
            public void run() {
                // TODO 自动生成的方法存根
                main.PlayerShenFuData.remove(p);

            }
        }.runTaskAsynchronously(main.plugin);
    }

    @EventHandler
    public void onPlayerEevent(GermGuiClickEvent e) throws IOException {
        if (e.getClickType().equals(GermGuiScreen.ClickType.LEFT_CLICK)){
            if (e.getClickedGuiScreen().getGuiName().equalsIgnoreCase("shenfu")) {
                if (e.getClickedPart().getIndexName().equalsIgnoreCase("升级_button")) {
                    Player p = e.getPlayer();
                    if (handle.updatePlayerLevel(p)) {
                        Germ.RebuildGui(e.getClickedGuiScreen(), p);

                    } else {
                        {
                            e.getClickedGuiScreen().close();
                            p.sendMessage("§a钱不够");
                        }
                    }
                }

            }
    }

    }
}
